package com.app.controller.backstage;


import com.app.model.AdminUserModel;
import com.app.model.PositionModel;
import com.app.service.AdminUserService;
import com.app.service.PositionService;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.StringTool;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RestController
public class PositionIndustryController {

    @Autowired
    public PositionService positionService;

    @Autowired
    UserService userService;

    @Autowired
    AdminUserService adminUserService;



    //查询三级职位或二级行业
    @GetMapping("getFindJobNameList")
    public Result<Object> generateFindJobName() {
        int postType = 1;
        List<PositionModel> modelList = positionService.findName(1, postType);
            for (int i = 0; i < modelList.size(); i++) {
                List<PositionModel> positionList = positionService.findNameJid(modelList.get(i).getPostCode(), postType);
                modelList.get(i).setSubLevelModelList(positionList);
                for (int j = 0; j < positionList.size(); j++) {
                    List<PositionModel> positionList2 = positionService.findNameJid(positionList.get(j).getPostCode(), postType);
                    if (positionList2 != null && positionList2.size() > 0) {
                        positionList.get(j).setSubLevelModelList(positionList2);
                    }
                }
        }

        return Result.success(modelList);
    }

    @GetMapping("getFindJobNameOne")
    public Result<Object> getFindJobNameOne() {
        List<PositionModel> modelList = positionService.findName(1, 1);
        return Result.success(modelList);
    }

    @GetMapping("getFindJobNameTwo")
    public Result<Object> getFindJobNameTwo(String fatherCode) {
        List<PositionModel> modelList = positionService.findNameJid(fatherCode,1);
        return Result.success(modelList);
    }

    @PostMapping("addFindJobName")
    public Result<Object> addFindJobName(@RequestParam(required = false) String classOne,
                                         @RequestParam(required = false) String classTwo,
                                         @RequestParam(required = false) String title,
                                         @RequestParam(required = false) String postCode,
                                         @RequestParam(required = false) Integer id,
                                         HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }
        AdminUserModel adminUser = adminUserService.getUserById(Integer.parseInt(userId));
        if(adminUser==null)
        {
            return Result.error(405,"用户不存在");
        }


        PositionModel model = new PositionModel();
        model.setJobTitle(title);
        model.setPostType(1);
        model.setPostCode(postCode);

        if(StringTool.isNotEmpty(classTwo))
        {
            model.setLevel(3);
            model.setFatherCode(classTwo);
        }else {
            model.setLevel(2);
            model.setFatherCode(classOne);
        }
        if(id!=null&&id>0)
        {
            model.setId(id);
            positionService.update(model);
        }else {
            PositionModel position = positionService.getModelByCode(postCode);
            if(position!=null)
            {
                return Result.error(500,"编号已存在");
            }
            model.setCrtTime(CommonHelp.getNowTimestamp());
            positionService.insert(model);
        }

        return Result.success("操作成功");
    }


    @GetMapping("getFindJobNameById")
    public Result<Object> getFindJobNameById(
                                         @RequestParam(required = false) Integer id,
                                         HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }
        AdminUserModel adminUser = adminUserService.getUserById(Integer.parseInt(userId));
        if(adminUser==null)
        {
            return Result.error(405,"用户不存在");
        }
        PositionModel position = positionService.getModelById(id);

        if(position.getLevel()==2)
        {
            position.setClassOne(position.getFatherCode());
        }

        if(position.getLevel()==3)
        {
            position.setClassTwo(position.getFatherCode());
            PositionModel positionModel = positionService.getModelByCode(position.getFatherCode());
            position.setClassOne(positionModel.getFatherCode());
        }
        return Result.success(position);
    }

    @GetMapping("deleteFindJobName")
    public Result<Object> deleteFindJobName(
            @RequestParam(required = false) Integer id,
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }
        AdminUserModel adminUser = adminUserService.getUserById(Integer.parseInt(userId));
        if(adminUser==null)
        {
            return Result.error(405,"用户不存在");
        }
        PositionModel position = positionService.getModelById(id);

        if(position.getLevel()==1)
        {
            List<PositionModel> modellist =  positionService.findNameJid(position.getPostCode(),1);
            for (int i = 0; i < modellist.size(); i++) {
                positionService.deleteByFatherCode(modellist.get(i).getPostCode());
            }
            positionService.deleteByFatherCode(position.getPostCode());
            positionService.delete(position);

        }
        if(position.getLevel()==2)
        {
            positionService.deleteByFatherCode(position.getPostCode());
            positionService.delete(position);
        }
        if(position.getLevel()==3)
        {
            positionService.delete(position);
        }
        return Result.success("操作成功");
    }

}
